package com.qiangesoft.datasource.controller;

import com.qiangesoft.datasource.core.DataSource;
import com.qiangesoft.datasource.core.DataSourceType;
import com.qiangesoft.datasource.entity.SysUser;
import com.qiangesoft.datasource.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author qiangesoft
 * @since 2024-03-14
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @DataSource(DataSourceType.MASTER)
    @GetMapping("/master")
    public List<SysUser> listMaster() {
        return sysUserService.list();
    }

    @DataSource(DataSourceType.SLAVE)
    @GetMapping("/slave")
    public List<SysUser> listSlave() {
        return sysUserService.list();
    }

    @DataSource(DataSourceType.MASTER)
    @GetMapping("/master/add")
    @Transactional
    public void addMaster() {
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(1L);
        sysUser.setUserName("test");
        sysUser.setPassword("123456");
        sysUser.setNickName("张三");
        sysUser.setStatus("1");
        sysUser.setDelFlag(false);
        sysUserService.save(sysUser);

        int i = 1 / 0;
    }

    @DataSource(DataSourceType.SLAVE)
    @GetMapping("/slave/add")
    @Transactional
    public void addSlave() {
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(1L);
        sysUser.setUserName("test");
        sysUser.setPassword("123456");
        sysUser.setNickName("张三");
        sysUser.setStatus("1");
        sysUser.setDelFlag(false);
        sysUserService.save(sysUser);

        int i = 10 / 0;
    }

}
